package com.pay.game.central.web.service;

import com.github.pagehelper.Page;
import com.pay.game.central.utils.DateTimeUtil;
import com.pay.game.central.web.bean.GroupOrderId;
import com.pay.game.central.web.bean.PddGoods;
import com.pay.game.central.web.mapper.GoodsMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class GoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    public void addOrUpdateGoods(PddGoods pddGoods){
        PddGoods temp = goodsMapper.queryById(pddGoods.getId());
        if (temp == null) {
            int index = goodsMapper.countGoods() + 1;
            goodsMapper.addGoods(
                    index,
                    pddGoods.getId(),
                    pddGoods.getGoods_name(),
                    pddGoods.getPrice(),
                    pddGoods.getThumb_url(),
                    pddGoods.getLink_url(),
                    pddGoods.getStore_id(),
                    pddGoods.getSku_id(),
                    pddGoods.getGroup_id(),
                    pddGoods.getGroup_id1(),
                    pddGoods.getPrice1(),
                    pddGoods.getUpdate_time());
        }else {
            goodsMapper.updateGoods(pddGoods);
        }
    }

    public void setGoodsSkuAndGroupAndPrice(String id, String skuId, String groupId,int price){
        goodsMapper.setGoodsSkuAndGroupAndPrice(id, skuId, groupId, price);
    }

    public String getFirstId(){
        return goodsMapper.getFirstId();
    }

    public String getNextId(String currentId){
        return goodsMapper.queryNextId(currentId);
    }

    public List<PddGoods> getAllGoodsWithStoreName(){
        return goodsMapper.getAllGoodsWithStoreName();
    }

    public List<PddGoods> getGoodsByPrice(int price){
        return goodsMapper.getGoodsByPrice(price);
    }

    public Page<Map<String,Object>> list(String id, String store_id, String store_name, int price, String goods_name, String user_name){
        return goodsMapper.list(id, store_id, store_name, price, goods_name, user_name, DateTimeUtil.getCurrentDate().getTime());
    }

    public void setLoop(String id,boolean loop){
        goodsMapper.setLoop(id, loop);
    }

    public List<Map<String,Object>> getOrderGoodsByPrice(int price){
        return goodsMapper.getOrderGoodsByPrice(price);
    }

    public List<Map<String,Object>> getOrderGoods(int price, int range, String userId){
        int ltPrice = price - range * 100;
        if (ltPrice < 1) ltPrice = 1;
        return goodsMapper.getOrderGoods(ltPrice, price, userId);
    }

    public List<Map<String, Object>> getStoreDonSize(List<String> ids){
        return goodsMapper.getStoreDonSize(ids);
    }

    public List<Map<String,Object>> getOrderGoodsPd(int price, int range, String userId){
        int ltPrice = price - range * 100;
        if (ltPrice < 1) ltPrice = 1;
        return goodsMapper.getOrderGoodsPd(ltPrice, price, userId);
    }

    public void setOrderTime(String id,long orderTime){
        goodsMapper.setOrderTime(id, orderTime);
    }

    public void increaseLoopTime(String id){
        goodsMapper.increaseLoopTime(id);
    }

    public void increaseDoneTimeAndSetPayTime(String id, int payTime){
        goodsMapper.increaseDoneTimeAndSetPayTime(id, payTime);
    }

    /**
     * 获取商品店铺用户信息
     */
    public Map<String, Object> getGoodsDetailInfo(String goodsId){
        return goodsMapper.getGoodsDetailInfo(goodsId);
    }

    public long getCurrentSettlementID(String id){
        return goodsMapper.getCurrentSettlementID(id);
    }

    public void setGroupOrderId(String id, String groupOrderId){
        goodsMapper.setGroupOrderId(id, groupOrderId);
    }

    public void addGroupOrderId(String goodsId, String id){
        goodsMapper.addGroupOrderId(goodsId, id, 1);
    }

    public void increaseGroupOrderIdTimes(String id){
        goodsMapper.increaseGroupOrderIdTimes(id);
    }

    public List<GroupOrderId> getGroupOrderId( String goodsId){
        return goodsMapper.getGroupOrderId(goodsId);
    }

    public void insertOrIncreaseGroupOrderId(String id, String goodsId){
        if (null != goodsMapper.getGroupOrderIdById(id)){
            goodsMapper.increaseGroupOrderIdTimes(id);
        } else {
            goodsMapper.addGroupOrderId(goodsId, id, 1);
        }
    }
}
